package com.springboot.love.controller;

import com.alibaba.fastjson.JSONObject;
import com.springboot.love.dao.AnswerDao;
import com.springboot.love.dao.PostDao;
import com.springboot.love.entity.Answer;
import com.springboot.love.entity.Post;
import com.springboot.love.service.AnswerService;
import com.springboot.love.service.PostService;
import com.springboot.love.utils.PageUtil;
import com.springboot.love.utils.StringHtml;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import net.sf.json.JSONArray;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
@Slf4j
public class PostController {

    @Resource
    private PostService postService;
    @Resource
    private PostDao postDao;
    @Resource
    private AnswerService answerService;
    @Resource
    private AnswerDao answerDao;

    /**
     * 查询所以帖子
     * @return
     */
    @RequestMapping(value = {"/postList"}, method = RequestMethod.GET)
    @ResponseBody
    public String postList(Model model,String page, String limit){
        log.info("查询全部帖子");
        PageUtil pageUtil=new PageUtil(Integer.parseInt(page),Integer.parseInt(limit));
        List<Post> postList = postService.queryAllByTime(pageUtil);
        for (Post p :postList){
            String s = StringHtml.delHtmlTags(p.getPostContent());
            p.setPostContent(s);
        }
        int postTotal=postService.queryTotalByTime();
        JSONObject json = new JSONObject();
        json.put("code",0);
        json.put("data",postList);
        json.put("count", postTotal);
        json.put("status", 200);
        return json.toString();
    }

    /**
     * 根据id修改帖子状态
     */
    @RequestMapping(value = {"/postStatus"}, method = RequestMethod.POST)
    public String  postStatus(String postId,String postStatus){
        log.info(postStatus.equals("1")?"发布成功":"待审核");
        Post post= new Post();
        post.setPostId(Integer.parseInt(postId));
        post.setPostState(Byte.parseByte(postStatus));
        postDao.updateById(post);
        return "admin/listPost";
    }

    /**
     * 根据id删除帖子
     */
    @RequestMapping(value = {"/postDelete"}, method = RequestMethod.POST)
    @ResponseBody
    public String postDelete(String postId){
        log.info("进入删除中...");
        int i = postDao.deleteById(Integer.parseInt(postId));
        JSONObject jsonObject = new JSONObject();
        if (i>0){
            log.info("删除帖子成功--帖子ID："+postId);
            jsonObject.put("success",true);
        }
        else {
            log.info("删除帖子失败--帖子ID："+postId);
            jsonObject.put("success",false);
        }
        return jsonObject.toString();
    }

    /**
     * 批量删除帖子
     */
    @RequestMapping("/postDeletes")
    public String  postDeletes(String delete){
        List<Post> list2=(List<Post>) JSONArray.toList(JSONArray.fromObject(delete), Post.class);
        List<Integer> postList = new ArrayList<>();
        JSONObject jsonObject = new JSONObject();
        for (Post post : list2){
            postList.add(post.getPostId());
        }
        List<Answer> bypostId = answerService.findBypostId(postList);
        int i = 0;
        if (bypostId !=null){
            List<Integer> answerList = new ArrayList<>();
            for (Answer answer : bypostId){
                answerList.add(answer.getAnswerId());
            }
            i = answerDao.deleteBatchIds(answerList);
        }
        int deletes = postDao.deleteBatchIds(postList);
        if (i>0 && deletes>0) {
            jsonObject.put("success",true);
        }else {
            jsonObject.put("success",false);
        }
        return jsonObject.toString();
    }

}
